package thinkingInJava.generics.wildcard;

//: generics/GenericWriting.java
import java.util.*;

public class GenericWriting {
static <T> void writeExact(List<T> list, T item) {
  list.add(item);
}
static List<Apple> apples = new ArrayList<Apple>();
static List<Fruit> fruit = new ArrayList<Fruit>();
static List<Jonathan> jonathan = new ArrayList<Jonathan>();
static void f1() {
  writeExact(apples, new Apple());
  // writeExact(fruit, new Apple()); // Error:
  // Incompatible types: found Fruit, required Apple
}
static <T> void
writeWithWildcard(List<? super T> list, T item) {
//writeWithWildcard(List<? extends T> list, T item) { // 如果用这个则下面那句出错
  list.add(item);
}	
static void f2() {
  writeWithWildcard(fruit, new Fruit());
  //writeWithWildcard(apples, new Fruit()); // Error 此时 T为 Fruit,所以 list 只能是 Fruit
  
  writeWithWildcard(apples, new Apple());
  writeWithWildcard(fruit, new Apple());
  //writeWithWildcard(jonathan, new Apple()); // Error T为 Apple, list 为 Apple 往上的类
  
  writeWithWildcard(apples, new Jonathan());
  writeWithWildcard(jonathan, new Jonathan());
  writeWithWildcard(fruit, new Jonathan());
}
public static void main(String[] args) { f1(); f2(); }
} ///:~
